﻿﻿/* =====================================================================================================================
  * Author - Sachin Sharma
  * Copyright (c) 2012 Sachin Sharma (3screator.com). All rights reserved.
  * Follows most coding rules. However, due to time constraint you will see some hard coding or rules not strictly followed
=====================================================================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpellCheckerWinRTComponent
{
    public class SpellChecker
    {
        static SpellChecker _SpellChecker = null;

        public static SpellChecker GetSpellChecker()
        {
            if (_SpellChecker == null)
            {
                _SpellChecker = new SpellChecker();
            }
            return _SpellChecker;
        }

        SpellChecker()
        {
        }

        public bool GetSupportedLanguages(out IList<string> lstAvailablleLangs)
        {
            lstAvailablleLangs = new List<string>();
            SpellCheckerWrapperComponent.SpellChecker spellChecker = new SpellCheckerWrapperComponent.SpellChecker();
            IList<string> lstAllAvailablleLangs = spellChecker.GetSupportedLanguages();
            // Remove empty and duplicates
            foreach (string lang in lstAllAvailablleLangs)
            {
                if (lang.Trim() != string.Empty && lstAvailablleLangs.Contains(lang) == false)
                {
                    lstAvailablleLangs.Add(lang.Trim());
                }
            }
            return (spellChecker.Errors.Count == 0 ? true : false);
        }

        public SpellCheckerWrapperComponent.SpellingCheckResults CheckWordSpelling(String word, String language)
        {
            SpellCheckerWrapperComponent.SpellingCheckResults spellingCheckResults = new SpellCheckerWrapperComponent.SpellingCheckResults();
            if (word == string.Empty) return spellingCheckResults;

            SpellCheckerWrapperComponent.SpellChecker spellChecker = new SpellCheckerWrapperComponent.SpellChecker();
            spellingCheckResults = spellChecker.CheckWordSpelling(word, language/*"en-US"*/);
            return spellingCheckResults;
        }
    }
}
